package asteroids.model.programs;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import asteroids.model.Ship;
import asteroids.model.programs.expressions.Expression;
import asteroids.model.programs.statements.Statement;
import asteroids.model.programs.statements.StatementList;
import asteroids.model.programs.statements.StatementRunner;
import asteroids.model.programs.types.Type;

public class Program {

	public Program(Map<String, Type> globals, Statement statement) {
		this.globals = globals;
		appendNextStatement(statement);
		assignments = new HashMap<String, Expression>();
	}
	
	public void resume() {
		sr.release();
		StatementList continueStatement = new StatementList(0,0,nextStatements);
		nextStatements = new LinkedList<Statement>();
		continueStatement.accept(sr);
	}

	public void setup(Ship ship) {
		this.sr = new StatementRunner(ship, this);
	}
	
	public void appendNextStatement(Statement s) {
		nextStatements.addLast(s);
	}
	
	private LinkedList<Statement> nextStatements  = new LinkedList<Statement>();
	
	private	StatementRunner sr;
	
	public Expression getAssignment(String key) {
		return getAssignments().get(key);
	}
	
	public void putAssignment(String key, Expression value) {
		getAssignments().put(key, value);
	}
	
	public Map<String, Expression> getAssignments() {
		return assignments;
	}
	
	private Map<String, Expression> assignments;
	
	public Type getType(String key) {
		return getGlobals().get(key);
	}
	
	public Map<String, Type> getGlobals() {
		return globals;
	}
	
	private final Map<String, Type> globals;
	
}
